package com.agile.leetcode.canculateObjectSize.unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * unsafe工具类
 *
 * @Author ChenZhangKun
 * @Date 2020/12/25 13:28
 */
public class UnsafeUtils {
    private UnsafeUtils() {
    }

    public static Unsafe unsafe;

    public static Unsafe getUnsafe() {
        if (unsafe != null) {
            return unsafe;
        }
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return unsafe;
    }
}
